/**
 * @class Ext.app.QueryPanel
 * @extends Ext.panel.Panel
 */
Ext.define('Ext.app.QueryPanel', {
    extend: 'Ext.tab.Panel',
    alias: 'widget.queryPanel',
    border: false,
    id: 'queryPanel',
    items: [],

    constructor: function (config) {
        this.newTabTitle = config.newTabTitle;
        this.loadQueryFunction = config.loadQueryFunction;
        
        this.callParent(arguments);
    },

    initComponent : function() {
        this.callParent();
    },
    
    afterFirstLayout: function() {
        this.callParent(arguments);
        this.addTab();
        //var lastTab = this.addTab();
        //lastTab.id = 'LAST_TAB';
        //lastTab.setTitle('+');
        
        this.setActiveTab(0);

        var loadQueryFunction = this.loadQueryFunction;
        var body = this.body;
        this.formPanelDropTarget = new Ext.dd.DropTarget(body, {
            ddGroup: 'tree-to-form',
            notifyEnter: function(ddSource, e, data) {
                var selectedRecord = ddSource.dragData.records[0];
                if(!selectedRecord.get("leaf")) {
                    return;
                }
                //Add some flare to invite drop.
                body.stopAnimation();
                body.highlight();
            },
            notifyOver: function(ddSource, e, data) {
                var selectedRecord = ddSource.dragData.records[0];
                var css = Ext.baseCSSPrefix;
                if(selectedRecord.get("leaf")) {
                    css += 'dd-drop-ok';
                }
                else {
                    css += 'dd-drop-nodrop';
                }
                return css;
            },
            notifyDrop: function(ddSource, e, data) {
                var selectedRecord = ddSource.dragData.records[0];
                if(!selectedRecord.get("leaf")) {
                    return false;
                }
                loadQueryFunction.call(null, selectedRecord.get("id"));
                return true;
            }
        });
    },
    
    addTab: function() {
        this.add({
            title: this.newTabTitle,
            layout: 'border',
            border: false,
            bodyPadding: 10,
            items: [
                {
                    region: 'center',
                    xtype: 'textarea',
                    autoScroll: true
                }
            ],
            listeners: {
                activate: function(tab) {
                    Ext.getCmp("parameterPanel").addFields(tab.parameters);
                },
                beforedeactivate: function(tab) {
                    tab.parameters = Ext.getCmp("parameterPanel").getAllFields();
                }
            }
        }).show();
    },
    
    setQuery: function(query) {
        var tabExisted = false;
        if(query.nquePk != null && query.nquePk != "") {
            var cmpArray = this.query('[nquePk=' + query.nquePk + ']');
            if(cmpArray.length > 0) {
                tabExisted = true;
                cmpArray[0].show();
            }
        }
        var tab = this.getActiveTab();
        var textarea = tab.down('textarea');
        
        if(!tabExisted && textarea.getValue() != null && textarea.getValue() != "") {
            this.addTab();
            tab = this.getActiveTab();
            textarea = tab.down('textarea');
        }
        tab.nquePk = query.nquePk;
        tab.setTitle(query.name);
        textarea.setValue(query.statement);
        if(query.nquePk != null && query.nquePk != "") {
            textarea.setReadOnly(query.isreadonly == 1);
        }
    },
    
    getQuery: function() {
        var tab = this.getActiveTab();
        var textarea = tab.down('textarea');
        return textarea.getValue();
    }
});
